package cate.game.play.param;

import cate.game.attr.FightAttr;
import cate.game.play.fighter.HeroFighter;
import cate.game.play.part.FightTeam;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.HashMap;
import java.util.Map;

@NoteClass("附加属性")
public class AttrAppend {

	@NoteField("针对全体目标增加的属性")
	private FightAttr teamAttr;

	@NoteField("针对特定tid英雄增加的属性")
	private Map<Integer, FightAttr> heroAttr;

	@NoteField("针对特定原型英雄增加的属性")
	private Map<Integer,FightAttr> heroProtoAttr;

	@NoteField("针对全体的属性缩放")
	private Double teamAttrScale;


	private FightAttr getOrBuildHeroAttr(int heroTid) {
		if (heroAttr == null) {
			heroAttr = new HashMap<>();
		}
		return heroAttr.computeIfAbsent(heroTid, k -> new FightAttr());
	}

	private FightAttr getHeroAttr(int heroTid) {
		if (heroAttr == null) {
			return null;
		}
		return heroAttr.get(heroTid);
	}


	private FightAttr getOrBuildHeroProtoAttr(int heroProtoId) {
		if (heroProtoAttr == null) {
			heroProtoAttr = new HashMap<>();
		}
		return heroProtoAttr.computeIfAbsent(heroProtoId, k -> new FightAttr());
	}

	private FightAttr getHeroProtoAttr(int heroProtoId) {
		if (heroProtoAttr == null) {
			return null;
		}
		return heroProtoAttr.get(heroProtoId);
	}


	/**
	 * 添加队伍属性(全队每个人都加)
	 * @param teamAttr
	 */
	public void addTeamAttr(FightAttr teamAttr) {
		if (this.teamAttr == null) {
			this.teamAttr = new FightAttr();
		}
		this.teamAttr.add(teamAttr);
	}

	/**
	 * 添加特定tid英雄的属性
	 * @param heroTid
	 * @param attr
	 */
	public void addHeroAttr(int heroTid, FightAttr attr) {
		getOrBuildHeroAttr(heroTid).add(attr);
	}

	/**
	 * 添加特定原型id英雄的属性
	 * @param heroProtoId
	 * @param attr
	 */
	public void addHeroProtoAttr(int heroProtoId, FightAttr attr) {
		getOrBuildHeroProtoAttr(heroProtoId).add(attr);
	}

	/**
	 * 添加属性放大系数
	 */
	public void setTeamAttrScale(double attrScale) {
		this.teamAttrScale = attrScale;
	}

	/** 处理队伍 */
	public void beforeFight(FightTeam ft) {
		for (HeroFighter f : ft.getAllHero()) {
			f.attr.addAttr(teamAttr);
			f.attr.addAttr(getHeroAttr(f.heroTid));
			f.attr.addAttr(getHeroProtoAttr(f.protoId));
			if (teamAttrScale != null && teamAttrScale > 0) {
				f.attr.scale(teamAttrScale);
			}
		}
	}
}
